﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewViews.Models;
using System.Collections;
//using NewViews.Models.User;
namespace NewViews.Controllers
{
    public class UserController : Controller
    {
        EShopDataEntities5 dbEntities = new EShopDataEntities5();
        //
        // GET: /User/
        Hashtable htProdInfo = new Hashtable();  

        public ActionResult Index()
        {
            object value = Session["key"];
            if (value != null)
            {
               
                var products = dbEntities.Products;
                ViewBag.product = products.ToList();
                return View();
            }
            return RedirectToAction("Login", "Account"); 
           
           
        }
        public ActionResult Details(int id=0)
        {
             object value = Session["key"];
             if (value != null)
             {

                 var products = dbEntities.Products;
                 ViewBag.prod = products.Find(id);
                 Product dbPprod = products.Find(id);
                 if (dbPprod == null)
                 {
                     return HttpNotFound();
                 }
                 return View(dbPprod);
             }
             return RedirectToAction("Login", "Account"); 
           
        }

        public ActionResult Cart(int id=0)
        {
             object value = Session["key"];
             if (value != null)
             {

                 var products = dbEntities.Products;
            ViewBag.prod = products.Find(id);
            Product dbPprod = products.Find(id);
            if (dbPprod == null)
            {
                return HttpNotFound();
            }
            return View(dbPprod);
             }
             return RedirectToAction("Login", "Account"); 
        }
        
        [HttpPost]
        
        public ActionResult CheckOut(Product model)
        {
            object value = Session["key"];
            if (value != null)
            {
                int id=model.Product_Id;
               
                var products = dbEntities.Products;
                ViewBag.X = products.Find(id);
                Product prodObj = products.Find(id);
                ViewBag.ProdId = prodObj.Product_Id;
                ViewBag.ProdName = prodObj.Product_Name;
                htProdInfo.Add("ProdName", prodObj.Product_Name);
                htProdInfo.Add("ProdId", prodObj.Product_Id);
                int price = Convert.ToInt32(prodObj.Product_Price);
                int amount = Convert.ToInt32(Request["p_quantity"]);
                ViewBag.ProdQuantity = amount;
                amount = price * amount;
                ViewBag.ProdAmount = amount;
                htProdInfo.Add("ProdAmount", amount);
                Session.Add("CartID", htProdInfo);

                return View();

            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult DoneShopping(Order orderObj)
        {
            object value = Session["key"];
            if (value != null)
            {
                orderObj.User_mail = (string) value;
                orderObj.OrderDate = DateTime.Now;
                dbEntities.Orders.Add(orderObj);
                dbEntities.SaveChanges();
                return View();
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult Laptops()
        {
            object value = Session["key"];
            if (value != null)
            {
               
                var products = dbEntities.Products;
                ViewBag.product = products.ToList();
                return View();
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult WebCam()
        {
            object value = Session["key"];
            if (value != null)
            {
               
                var products = dbEntities.Products;
                ViewBag.product = products.ToList();
                return View();
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult Mobile()
        {
            object value = Session["key"];
            if (value != null)
            {
               
                var products = dbEntities.Products;
                ViewBag.product = products.ToList();
                return View();
            }
            return RedirectToAction("Login", "Account");
        }
        public ActionResult Search()
        {
            object value = Session["key"];
            if (value != null)
            {
                string searchString = Request["SearchItems"];
                var prods = from m in dbEntities.Products
                            select m;
                if (!String.IsNullOrEmpty(searchString))
                {
                    prods = prods.Where(s => s.Product_Name.Contains(searchString));
                    ViewBag.product = prods;
                    if (ViewBag.product == null)
                    {
                        return View("NoSearch", "User");
                       
                    }
                    else
                    {
                        return View();
                    }

                    
                }

                
            }
            return RedirectToAction("Login", "Account"); 
        }
        public ActionResult NoSearch()
        {
             object value = Session["key"];
             if (value != null)
             {

                 return View();
             }
             return RedirectToAction("Login", "Account"); 

        }
    }
    
}
